import 'package:financing_wallet/home/api/hxh_api.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'dart:convert';
import '../../product/api/product_api.dart';
import '../bean/hxhAuditResultBean.dart';
import '../bean/hxhOrderBean.dart';
import 'state.dart';

class HXHAuditLogic extends GetxController {
  final HXHAuditState state = HXHAuditState();
  List<OrderList>? orderList = [];
  num? enterType;
  @override
  void onInit() {
    super.onInit();
    getUserOrder(2);
    if (Get.arguments != null) {
      enterType=Get.arguments['enterType'];
    }
  }

  ///订单列表
  getUserOrder(int type) {
    HXHApi.getUserAllOrder(BaseCallback(onSuccess: (map) {
      HxhOrderBean orderBean = HxhOrderBean.fromJson(map);
      if (orderBean.data != null) {
        orderList = orderBean.data;
        if (orderList!.isNotEmpty || orderList != null) {
          state.orderNo = orderList![0].orderNo;
          getAuditResult(type);
        } else {
          ToastUtils.showToast(orderBean.msg ?? "");
        }
      } else {
        ToastUtils.showToast(orderBean.msg ?? "");
      }
      update();
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
        update();
      });
    }));
  }

  //审核结果
  getAuditResult(int type) {
    HXHApi.getAuditResult(
        state.orderNo!,
        BaseCallback(onSuccess: (map) {
          HxhAuditResultBean resultBean = HxhAuditResultBean.fromJson(map);
          if (resultBean.data != null) {
            state.institutionProductName =
                resultBean.data?.institutionProductName;
            state.auditTime = resultBean.data?.auditTime;
            state.status = resultBean.data?.status;
            if (type == 1) {

            } else {
              if (state.status == 131) {
                //审核成功
                Get.offNamed(BaseRouterConfig.hxhAuditPass);
              } else if (state.status == 110) {
                //审核中
                if(enterType==1){
                  ToastUtils.showToast("已加速审核进度,请稍后");
                }else{
                  Get.offNamed(BaseRouterConfig.loanRecord);
                }
              } else {
                //审核失败
                Get.offNamed(BaseRouterConfig.auditRejection);
              }
            }
            update();
          } else {
            //审核失败
            Get.toNamed(BaseRouterConfig.auditRejection);
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }
}
